﻿using System;
using System.Net;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.BusinessObjects;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.DataAccess
{
    public class TimeSlotsProvider : ObservableCollection<ucTimeSlotItem>
    {
        ucTimeSlots Owner = null;

        #region Constructor
        public TimeSlotsProvider(ucTimeSlots _Owner)
        {
            Owner = _Owner;
        }
        #endregion

        #region Methods
        #region GetTimeSlots
        public ObservableCollection<ucTimeSlotItem> GetTimeSlots()
        {
            // 22 = single item
            double TimeSlotHeight = 22;
            int LoopMinutes = 60 / (int)Owner.TimeSlotsPerHour;
            int LoopHours = 24 / (int)Owner.HoursPerDay;

            TimeSpan tsStart = new TimeSpan(0, 0, 0);
            TimeSpan tsEnd = new TimeSpan(24, 0, 0);
            TimeSpan tsTimeScale = new TimeSpan(LoopHours, 0, 0);

            while (tsStart < tsEnd)
            {
                ucTimeSlotItem ucTs = new ucTimeSlotItem();
                HourIsNowTime(ref ucTs, tsStart.Hours);

                switch (tsStart.Hours)
                {
                    case 0:
                        ucTs.BorderThickness = new Thickness(0);
                        ucTs.Hour = "12";
                        ucTs.Minute = "AM";
                        break;
                    case 12:
                        ucTs.Hour = "12";
                        ucTs.Minute = "PM";
                        break;
                    default:
                        if (tsStart.Hours > 12)
                        {
                            ucTs.Hour = (tsStart.Hours - 12).ToString();
                            ucTs.Minute = "00";
                        }
                        else
                        {
                            ucTs.Hour = tsStart.Hours.ToString();
                            ucTs.Minute = "00";
                        }
                        break;
                }

                ucTs.Height = (TimeSlotHeight * LoopMinutes) * LoopHours;

                this.Add(ucTs);

                tsStart += tsTimeScale;                
            }

            return this;
        }
        #endregion

        #region HourIsNowTime
        private void HourIsNowTime(ref ucTimeSlotItem ucTS, int Hour)
        {
            if (Hour == DateTime.Now.Hour)
            {
                ucTS.Background = Owner.CurrentTimeBackground;
                ucTS.BorderBrush = Owner.CurrentTimeBorderBrush;
                ucTS.BorderThickness = Owner.CurrentTimeBorderThickness;
            }
            else
            {
                ucTS.Background = Owner.ContentBackground;
                ucTS.BorderBrush = Owner.ContentBorderBrush;
                ucTS.BorderThickness = Owner.ContentBorderThickness;
            }
        }
        #endregion
        #endregion
    }
}
